<?php

namespace app\hotel\controller;

use think\Controller;

class Guest extends Controller
{
    protected $BusinessModel;
    protected $GuestModel;
    protected $PayModel;
    protected $Business;

    public function __construct()
    {
        parent::__construct();

        // 公共区域加载模型
        $this->BusinessModel = model('Business.Business');
        $this->GuestModel = model('Hotel.Guest');

        $busid = $this->request->param('busid', '', 'trim');

        $business = $this->BusinessModel->where(['id' => $busid])->find();

        if (!$business) {
            $this->error('用户不存在');
            exit;
        }

        $this->Business = $business;
    }

    public function index()
    {
        $page = $this->request->param('page', 1, 'trim');
        $limit = $this->request->param('limit', 0, 'trim');
        $keyword = $this->request->param('keyword', '', 'trim');

        if (!$limit) {
            $limit = 10;
        }

        // 分页偏移量
        $offset = ($page - 1) * $limit;

        $where = ['busid' => $this->Business['id']];

        if ($keyword) {
            $where['nickname'] = ['like', '%' . $keyword . '%'];
        }

        $total = $this->GuestModel
            ->where($where)
            ->count();

        $list  = $this->GuestModel
            ->where($where)
            ->order('id', 'desc')
            ->limit($offset, $limit)
            ->select();

        $result = [
            'list' => $list ?? [],
            'total' => $total
        ];

        $this->success('ok', null, $result);
    }

    public function add()
    {
        $nickname = $this->request->param('nickname', '', 'trim');
        $mobile   = $this->request->param('mobile', '', 'trim');
        $gender   = $this->request->param('gender', '', 'trim');

        if (empty($nickname)) {
            $this->error('请输入住客姓名');
            exit;
        }

        if (empty($mobile)) {
            $this->error('请输入手机号');
            exit;
        }

        if ($gender !== '0' || $gender !== '1') {
            $this->error('性别错误');
            exit;
        }

        $data = [
            'nickname' => $nickname,
            'mobile'   => $mobile,
            'gender'   => $gender,
            'busid'    => $this->Business['id'],
        ];

        $result = $this->GuestModel->save($data);

        if ($result === false) {
            $this->error('添加失败');
            exit;
        } else {
            $this->success('添加成功');
        }
    }

    public function info()
    {
        $id = $this->request->param('id', 0, 'trim');

        $res = $this->GuestModel->find($id);

        if (empty($res)) {
            $this->error('住客信息不存在');
            exit;
        } else {
            $this->success('ok', null, $res);
            exit;
        }
    }

    public function edit()
    {
        $nickname = $this->request->param('nickname', '', 'trim');
        $mobile   = $this->request->param('mobile', '', 'trim');
        $gender   = $this->request->param('gender', '', 'trim');
        $id   = $this->request->param('id', 0, 'trim');

        if (empty($nickname)) {
            $this->error('请输入住客姓名');
            exit;
        }

        if (empty($mobile)) {
            $this->error('请输入手机号');
            exit;
        }

        if ($gender !== '0' && $gender !== '1') {
            $this->error('性别错误');
            exit;
        }

        $guest = $this->GuestModel->find($id);

        if (empty($guest)) {
            $this->error('住客信息不存在');
            exit;
        }

        $data = [
            'nickname' => $nickname,
            'mobile'   => $mobile,
            'gender'   => $gender,
            'id'       => $guest['id'],
        ];

        $result = $this->GuestModel->isUpdate(true)->save($data);

        if ($result === false) {
            $this->error('编辑失败');
            exit;
        } else {
            $this->success('编辑成功');
            exit;
        }
    }

    public function del()
    {
        $id   = $this->request->param('id', 0, 'trim');

        $guest = $this->GuestModel->find($id);

        if (empty($guest)) {
            $this->error('住客信息不存在');
            exit;
        }

        $res = $this->GuestModel->where(['id' => $id])->delete();

        if ($res === false) {
            $this->error('删除失败');
            exit;
        } else {
            $this->success('删除成功');
            exit;
        }
    }

    public function list()
    {
        $where = ['busid' => $this->Business['id']];

        $list  = $this->GuestModel
            ->where($where)
            ->order('id', 'desc')
            ->select();

        $this->success('ok', null, $list);
    }
}
